﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace DLC.Module.DataTable.ArchiveBytes
{
    public static partial class DataTableBytesExtension
    {
        [BinaryWriterHelper(typeof(float))]
        public static void WriteSingle(BinaryWriter writer, object data)
        {
            try
            {
                writer.Write((float)data);
            }catch(Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(float[]))]
        public static void WriteSingleArray(BinaryWriter writer, object data)
        {
            try
            {
                float[] value = (float[])data;
                writer.Write(value.Length);
                foreach (var val in value)
                {
                    writer.Write(val);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(double))]
        public static void WriteDouble(BinaryWriter writer, object data)
        {
            try
            {
                writer.Write((double)data);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(double[]))]
        public static void WriteDoubleArray(BinaryWriter writer, object data)
        {
            try
            {
                double[] value = (double[])data;
                writer.Write(value.Length);
                foreach (var val in value)
                {
                    writer.Write(val);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }



        [BinaryWriterHelper(typeof(short))]
        public static void WriteInt16(BinaryWriter writer, object data)
        {
            try
            {
                writer.Write((short)data);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(short[]))]
        public static void WriteInt16Array(BinaryWriter writer, object data)
        {
            try
            {
                short[] value = (short[])data;
                writer.Write(value.Length);
                foreach (var val in value)
                {
                    writer.Write(val);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(int))]
        public static void WriteInt32(BinaryWriter writer, object data)
        {
            try
            {
                writer.Write((int)data);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(int[]))]
        public static void WriteInt32Array(BinaryWriter writer, object data)
        {
            try
            {
                int[] value = (int[])data;
                writer.Write(value.Length);
                foreach (var val in value)
                {
                    writer.Write(val);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(long))]
        public static void WriteInt64(BinaryWriter writer, object data)
        {
            try
            {
                writer.Write((long)data);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(long[]))]
        public static void WriteInt64Array(BinaryWriter writer, object data)
        {
            try
            {
                long[] value = (long[])data;
                writer.Write(value.Length);
                foreach (var val in value)
                {
                    writer.Write(val);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }
        [BinaryWriterHelper(typeof(string))]
        public static void WriteString(BinaryWriter writer, object data)
        {
            try
            {
                writer.Write((string)data);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }


        [BinaryWriterHelper(typeof(string[]))]
        public static void WriteStringArray(BinaryWriter writer, object data)
        {
            try
            {
                string[] values = (string[])data;
                writer.Write(values.Length);
                foreach (var value in values)
                {
                    writer.Write(value);
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }
        [BinaryWriterHelper(typeof(Enum))]
        public static void WriteEnum(BinaryWriter writer, object data)
        {
            try
            {
                string value = Enum.GetName(data.GetType(), data);
                writer.Write(value);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }
        [BinaryWriterHelper(typeof(byte))]
        public static void WriteByte(BinaryWriter writer, object data)
        {
            try
            {
                writer.Write((byte)data);
            }
            catch (Exception e)
            {
                Debug.LogError($"ReadByte error: {e.Message}. data is {data}");
            }
        }
        [BinaryWriterHelper(typeof(byte[]))]
        public static void WriteByteArray(BinaryWriter writer, object data)
        {
            try
            {
                byte[] values = (byte[])data;
                writer.Write(values.Length);
                writer.Write(values);
            }
            catch (Exception e)
            {
                Debug.LogError($"WriteSingle error: {e.Message}. data is {data}");
            }
        }
    }
}